import matplotlib.pyplot as plt
import numpy as np

# 1. 数据准备
methods = ['DTFL', 'PMS', 'FLTrust']
comm_cost = [38, 110, 205]  # 单位KB


# 2. 创建图表基础
plt.figure(figsize=(8, 5), dpi=900)  # 设置图像大小和分辨率
ax = plt.subplot()

# 3. 绘制柱状图
bars = ax.bar(methods, comm_cost,)

# # 4. 添加数据标签
# for i, bar in enumerate(bars):
#     height = bar.get_height()
#     ax.text(bar.get_x() + bar.get_width()/2., height+5, 
#             f'{height} KB', ha='center', va='bottom', fontsize=12, fontweight='bold')
    
#     # 为DTFL添加额外百分比说明
#     if i == 0:
#         ax.text(bar.get_x() + bar.get_width()/2., height+15, 
#                 '仅为PMS的35%', ha='center', va='bottom', fontsize=11, color='#D62728')

# 5. 添加基准线
ax.axhline(y=38, color='red', linestyle='--', alpha=0.3, xmin=0.05, xmax=0.95)

# 6. 坐标轴和标签设置
plt.ylabel('communication overhead (KB)', fontsize=15, labelpad=10)
plt.xticks(fontsize=15)
# plt.title('图4.3：每轮通信开销对比', fontsize=14, pad=15)
plt.ylim(0, 220)  # 设置y轴范围

# 7. 网格和样式优化
# ax.grid(axis='y', linestyle='--', alpha=0.7)
# plt.tick_params(axis='both', which='major', labelsize=11)
# plt.tight_layout()

# 8. 显示图表
plt.savefig('Fig3.png', dpi=300)
plt.show()
